package org.bbb.library1.wizards;

/*******************************************************************************
 * Copyright (c) 2007 Finalist IT Group B.V.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Rob Schellhorn - initial API and implementation
 *******************************************************************************/
//package com.finalist.rcp.internal.ui.wizards;

import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.dialogs.IMessageProvider;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;

import org.bbb.library1.RcpDemoPlugin;
import org.bbb.library1.model.Book;
import org.bbb.library1.model.Library;

/**
 * @author Rob Schellhorn
 */
public class NewBookWizard extends Wizard implements INewWizard {

   private class NewBookWizardPage extends WizardPage {
      public NewBookWizardPage() {
         super("new_book", "Wizard Page", null);
      }

      /*
       * @see org.eclipse.jface.dialogs.IDialogPage#createControl(org.eclipse.swt.widgets.Composite)
       */
      public void createControl(Composite parent) {
         Composite control = new Composite(parent, SWT.NONE);
         control.setLayout(new GridLayout(2, false));
         setControl(control);

         // Modify events just trigger a revalidation of all controls
         final ModifyListener listener = new ModifyListener() {
            public void modifyText(ModifyEvent e) {
               IStatus status = getStatus();
               switch (status.getSeverity()) {
                  // Hopefully IStatus will extend IMessageProvider in the near future, but for now:
                  case IStatus.INFO:
                     setMessage(status.getMessage(), IMessageProvider.INFORMATION);
                     setPageComplete(true);
                     break;
                  case IStatus.WARNING:
                     setMessage(status.getMessage(), IMessageProvider.WARNING);
                     setPageComplete(true);
                     break;
                  case IStatus.ERROR:
                     setMessage(status.getMessage(), IMessageProvider.ERROR);
                     setPageComplete(false);
                     break;
                  default:
                     setMessage(null);
                     setPageComplete(true);
                     break;
               }
            }
         };

         Label label = new Label(control, SWT.LEAD);
         label.setLayoutData(new GridData(SWT.BEGINNING, SWT.CENTER, false, false));
         label.setText("Title:");
         title = new Text(control, SWT.BORDER | SWT.LEAD | SWT.SINGLE);
         title.addModifyListener(listener);
         title.setLayoutData(new GridData(SWT.FILL, SWT.BEGINNING, true, false));



         setPageComplete(false);
      }

      /*
       * @see org.eclipse.jface.dialogs.DialogPage#dispose()
       */
      @Override
      public void dispose() {
         try {
            title.dispose();
            //publisher.dispose();
            //numberOfPages.dispose();
         } finally {
            super.dispose();
         }
      }

      private IStatus getStatus() {
         String title = NewBookWizard.this.title.getText();
         //String publisher = NewBookWizard.this.publisher.getText();
         //String numberOfPages = NewBookWizard.this.numberOfPages.getText().trim();

         if (title.trim().length() <= 0) {
            return new Status(IStatus.ERROR, RcpDemoPlugin.PLUGIN_ID,
                  "The title must be at least one character long.");
         }

         try {
            
         } catch (NumberFormatException e) {
            return new Status(IStatus.ERROR, RcpDemoPlugin.PLUGIN_ID,
                  "The number of pages must be a positive integer");
         }

         if (!title.equals(title.trim())) {
            return new Status(IStatus.WARNING, RcpDemoPlugin.PLUGIN_ID,
                  "Leading and trailing whitespace will be removed from the title automatically.");
         }

         

         return Status.OK_STATUS;
      }
   }

   public static final String WIZARD_ID = "com.finalist.rcp.wizards.new.book";

   private Text title;
   private Text publisher;
   private Text numberOfPages;

   /*
    * @see org.eclipse.jface.wizard.Wizard#addPages()
    */
   @Override
   public void addPages() {
      addPage(new NewBookWizardPage());
   }

   private Integer getNumberOfPages() {
      Integer result = null;

      try {
         String value = this.numberOfPages.getText().trim();
         if (!"".equals(value)) {
            result = Integer.parseInt(value);
         }
      } catch (NumberFormatException e) {
         // This should never happen, since the input has been validated and found to be correct
      }

      return result;
   }

   /*
    * @see org.eclipse.ui.IWorkbenchWizard#init(org.eclipse.ui.IWorkbench,
    *      org.eclipse.jface.viewers.IStructuredSelection)
    */
   public void init(IWorkbench workbench, IStructuredSelection selection) {/* TODO */}

   /*
    * @see org.eclipse.jface.wizard.Wizard#performFinish()
    */
   @Override
   public boolean performFinish() {
      Library library = RcpDemoPlugin.getDefault().getLibrary();

      Book book = new Book();
      book.setTitle(title.getText().trim());
      //book.setPublisher(publisher.getText().trim());
      //book.setNumberOfPages(getNumberOfPages());

      library.add(book);
      if (library.hasChanged()) {
         library.notifyObservers();
      }

      return true;
   }
}